﻿using System;
using System.IO;


namespace DailyFx.Book.Repo
{

	public class Router : IRouter
	{
		/// <summary>
		///		the table for this router .
		/// </summary>
		private TableImpl _table;

		/// <summary>
		///		the physical file/folder path .
		/// </summary>
		private string _fPath;

		/// <summary>
		///		Traits for table storage policy .
		/// </summary>
		private ITraits _traits ;

		/// <summary>
		///		the data reader .
		/// </summary>
		private CsvReader _reader = null ;


		/// <summary>
		///		c;tor 
		/// </summary>
		public Router ( TableImpl table )
		{
			this.InitImpl ( table);
		}

		/// <summary>
		///		Gets the table of this router .
		/// </summary>
		public ITable Table
		{
			get { return this._table; }
		}


		/// <summary>
		/// 
		/// </summary>
		private void  InitImpl ( TableImpl table )
		{
			this._table = table;


			string dir;
			if ( table.Symbol.Category == Symbol.Forex )
			{
				dir = Path.Combine( table.Symbol.Category, table.Symbol.Code );
			}
			else // by market .
			{
				dir = Path.Combine(table.Symbol.Category, table.Symbol.Market, table.Symbol.Code );
			}

			string fn = table.Label;
			if ( table.Metrics.Period >= Period.Daily )
			{
				// single file .
				this._traits = new HistoryTraits () ;
			}
			else if ( table.Metrics.Period <= Period.min5 )
			{
				// file(s) by daily .
				this._traits = new DailyTraits();
			}
			else
			{
				throw new NotImplementedException( "Unsupported type of table(s) .");
			}

			// todo:
			this._fPath = Path.Combine( table.Book.Root, table.Book.Label, dir, fn );
		}



		public bool TrySeek ( DateTime key )
		{
			throw new NotImplementedException("");
		}

		public bool TryMoveBack ()
		{
			throw new NotImplementedException( "Router can only move forward.");
		}

		public bool TryMoveNext ()
		{
			throw new NotImplementedException("");
		}

		public void Rewind ()
		{
			this._reader.Dispose();
			this._reader = null ;
		}

	}
}
